package com.photoeditor.demo.db;

import android.database.sqlite.SQLiteCantOpenDatabaseException;

import com.common.base.ComponentContext;
import com.photoeditor.demo.db.update.GreenDaoSQLiteOpenHelper;

import org.greenrobot.greendao.database.Database;

/**
 * Created by winn on 17/4/13.
 */

public class DBHelper {
    public static final boolean ENCRYPTED = false;

    private static DaoSession sDaoSession;
    private static DBHelper sDBHelper;

    public static DBHelper getInstance() {
        if (sDBHelper == null) {
            synchronized (DBHelper.class) {
                if (sDBHelper == null) {
                    sDBHelper = new DBHelper();
                }
            }
        }
        return sDBHelper;
    }

    private DBHelper() {
        try {
            // 线上问题，怀疑数据库的文件被删除导致的
            GreenDaoSQLiteOpenHelper helper = new GreenDaoSQLiteOpenHelper(ComponentContext.getContext(),
                    ENCRYPTED ? "app-db-encrypted" : "app-db");
            Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
            sDaoSession = new DaoMaster(db).newSession();
        } catch (SQLiteCantOpenDatabaseException exception) {
        }
    }

    public DaoSession getDaoSession() {
        return sDaoSession;
    }

}
